
/* File name	: adc.c
 * Project 		: BKIT MCR
 * Version 		: 1.0
 * Creation Date: 07/07/2010
 * Tabsize		: 4
 * Author  		: BKIT4U                   
 * Copyright	: (c) 2010 BKIT HARDWARE CLUB - www.bkit4u.com
 * License		: Free.
 * Comments		: 
 * 
 */

/*====================================*/
/*  Include                           */
/*====================================*/
#include <avr/io.h>
#include <util/delay.h>
#include "adc.h"
#include "../led/led.h"
#include "../eeprom/eeprom.h"

/*====================================*/
/*	Global Variable Declaration       */
/*====================================*/
unsigned char v_black[8];			
unsigned char v_white[8];
unsigned char v_compare[8];
unsigned char sensor_value[8];
unsigned char sensor, sensor1, sensor2, sensor0;

unsigned char isUpdate = 0;

/*====================================*/
/*  Function name: adc_init           */
/*  Parameter: null                   */
/*  Description: configure hardware   */
/*               for adc function     */
/*====================================*/
void adc_init(){

	// ADC initialization
    // ADC Clock frequency: 1000.000 kHz
    // ADC Voltage Reference: AVCC pin
    // Use 8-bit mode
    ADMUX=ADC_VREF_TYPE & 0xff;
    ADCSRA=0x84;
}

/*====================================*/
/*  Function name: read_adc           */
/*  Parameter: adc_input              */
/*  Return value: value of voltage    */
/*                after converting    */
/*  Description:1.return voltage level*/
/*               of adc_input led from*/
/*               sensor circuit       */
/*              2. AD converter       */
/*                  function          */
/*====================================*/
unsigned char read_adc(unsigned char adc_input)
{
	// Read the 8 most significant bits
	// of the AD conversion result

    ADMUX = adc_input | (ADC_VREF_TYPE & 0xff);

    // Delay needed for the stabilization of the ADC input voltage
    _delay_us(10);

    // Start the AD conversion
    ADCSRA|=0x40;

    // Wait for the AD conversion to complete
    while ((ADCSRA & 0x10)==0);

    ADCSRA|=0x10;
    return ADCH;
}


/*====================================*/
/*  Function name: read_volt          */
/*  Parameter: null                   */
/*  Description: save value of voltage*/
/*               for 8 sensor on      */
/*               sensor circuit       */ 
/*====================================*/
void read_volt()
{
	unsigned char i = 0;
    for (i = 0; i < 8; i++) 
        sensor_value[i] = read_adc(i);   
}

/*====================================*/
/*  Function name: read_sensor        */
/*  Parameter: null                   */
/*  Description: update current value */
/*               on sensor circuit    */
/*               and output to led    */
/*====================================*/
void read_sensor(){ 
    unsigned char temp = 0;
	unsigned char i = 0;
	sensor2 = sensor1;
	sensor1 = sensor0;
    if(isUpdate == 0){
        read_volt();
        for (i = 0; i < 8; i++){
            temp = temp << 1;  
            if (sensor_value[i] < v_compare[i]) 
                temp |= 0x01;
            else 
                temp &= 0xfe;
        }
        sensor0 = temp;         
    }
	if ((sensor2 == sensor1)&&(sensor1 == sensor0)){
		sensor = sensor0;
		// output state of sensor on sensor circuit at led module
        led_put(sensor);       
	}
}

/*====================================*/
/*  Function name: save_sensor        */
/*  Parameter: color                  */
/*  Description: save value of voltage*/
/*              for 8 led on "color"  */
/*              line in eeprom of MCU.*/
/*				 color is BLACK       */
/*				or WHITE			  */
/*====================================*/
void save_sensor(unsigned char color)
{    
	unsigned char i = 0;   
    isUpdate = 1;
    if(color == WHITE){								// color is WHITE
        for(i = 0; i < 8; i++){
            v_white[i] = read_adc(i);
        }
		eeprom_writestr(V_WHITE_POS, v_white, 8);	// write value into eeprom

    } else if (color == BLACK){						// color is BLACK
        for(i = 0; i < 8;i++){
            v_black[i] = read_adc(i);	
        }
		eeprom_writestr(V_BLACK_POS, v_black, 8);	// write value into eeprom
    }
	update_vcompare();								// update value of v_compare[8]
    isUpdate = 0;
}

/*====================================*/
/*  Function name: update_vcompare    */
/*  Parameter: null                   */
/*  Description: update value of      */
/*               v_compare[8] and save*/
/*               in eeprom of MCU     */
/*====================================*/
void update_vcompare(){
    unsigned char i; 
    for(i = 0; i < 8 ; i++){ 
        v_compare[i] = v_white[i] + 10;
    }
	write_vcompare(); 
}

/*====================================*/
/*  Function name: read_vcompare      */
/*  Parameter: null                   */
/*  Description: read v_compare[8]    */
/*               from eeprom          */
/*====================================*/
void read_vcompare(){
	eeprom_readstr(V_COMPARE_POS, v_compare, 8);
}

/*====================================*/
/*  Function name: read_vcompare      */
/*  Parameter: null                   */
/*  Description: write v_compare[8]   */
/*               into eeprom          */
/*====================================*/
void write_vcompare(){
	eeprom_writestr(V_COMPARE_POS, v_compare, 8);
}


/****************END FILE**************/

